package net.tv90.appserveradmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.tv90.appserveradmin.dao.CheckitemDetailedMapper;
import net.tv90.appserveradmin.dao.CheckitemMapper;
import net.tv90.appserveradmin.model.Checkitem;
import net.tv90.appserveradmin.model.CheckitemDetailed;
import net.tv90.appserveradmin.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class CheckitemServiceImpl implements CheckitemService {

    @Autowired
    private CheckitemMapper checkitemMapper;

    @Autowired
    private CheckitemDetailedMapper checkitemDetailedMapper;

    @Override
    public List<Checkitem> list() {
        return checkitemMapper.selectList(null);
    }

    @Override
    public Checkitem getById(Long id) {
        return checkitemMapper.selectById(id);
    }

    @Transactional
    @Override
    public boolean save(Checkitem checkitem, CheckitemDetailed checkitemDetailed) {
        checkitemMapper.insert(checkitem);
        checkitemDetailed.setCiId(checkitem.getCiId());
        checkitemDetailedMapper.insert(checkitemDetailed);
        return true;
    }

    @Transactional
    @Override
    public boolean updateById(Checkitem checkitem, CheckitemDetailed checkitemDetailed) {
        checkitemMapper.updateById(checkitem);
        checkitemDetailedMapper.updateById(checkitemDetailed);
        return true;
    }

    @Transactional
    @Override
    public boolean deleteById(Long id) {
        checkitemMapper.deleteById(id);
        checkitemDetailedMapper.deleteByMap(Map.of("ciId", id));
        return true;
    }

    @Override
    public CheckitemDetailed getByCiId(Long ciId) {
        QueryWrapper<CheckitemDetailed> wrapper = new QueryWrapper<>();
        wrapper.eq("ciId", ciId);
        return checkitemDetailedMapper.selectOne(wrapper);
    }
}
